#include "stdafx.h"
#include "MyIExampleEvts.h"
#include "MyConnectionPoint2.h"

HRESULT STDMETHODCALLTYPE MyConnectionPoint2::QueryInterface(REFIID riid, void **ppv)
{
    MyIExampleEvts *p = CONTAINING_RECORD(this, MyIExampleEvts, point2);
    return p->example.QueryInterface(riid, ppv);
}

ULONG STDMETHODCALLTYPE MyConnectionPoint2::AddRef()
{
    MyIExampleEvts *p = CONTAINING_RECORD(this, MyIExampleEvts, point2);
    return p->example.AddRef();
}

ULONG STDMETHODCALLTYPE MyConnectionPoint2::Release()
{
    MyIExampleEvts *p = CONTAINING_RECORD(this, MyIExampleEvts, point2);
    return p->example.Release();
}

HRESULT STDMETHODCALLTYPE MyConnectionPoint2::GetConnectionInterface(IID *pIID)
{
    memcpy(pIID, &DIID_ICompare, sizeof(IID));
    return S_OK;
}

HRESULT STDMETHODCALLTYPE MyConnectionPoint2::GetConnectionPointContainer(IConnectionPointContainer **ppCPC)
{
    MyIExampleEvts *p = CONTAINING_RECORD(this, MyIExampleEvts, point2);
    *ppCPC = &p->container;
    p->container.AddRef();
    return S_OK;
}

HRESULT STDMETHODCALLTYPE MyConnectionPoint2::Advise(IUnknown *pUnkSink, DWORD *pdwCookie)
{
    MyIExampleEvts *p = CONTAINING_RECORD(this, MyIExampleEvts, point2);
    if (p->compare) return CONNECT_E_ADVISELIMIT;
    HRESULT hr = pUnkSink->QueryInterface(DIID_ICompare, (void **)&p->compare);
    *pdwCookie = (DWORD)p->compare;
    return hr;
}

HRESULT STDMETHODCALLTYPE MyConnectionPoint2::Unadvise(DWORD dwCookie)
{
    MyIExampleEvts *p = CONTAINING_RECORD(this, MyIExampleEvts, point2);
    if (dwCookie && (ICompare *)dwCookie == p->compare)
    {
        p->compare->Release();
        p->compare = NULL;
        return S_OK;
    }
    return CONNECT_E_NOCONNECTION;
}

HRESULT STDMETHODCALLTYPE MyConnectionPoint2::EnumConnections(IEnumConnections **ppEnum)
{
    *ppEnum = NULL;
    return E_NOTIMPL;
}
